<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <eHeader :dict="dict" :permission="permission"/>
      <crudOperation :permission="permission"/>
    </div>
    <!--表格渲染-->
    <el-table ref="table" v-loading="crud.loading" :data="crud.data" class="limit-height" height="600"
              style="width: 100%;" @selection-change="crud.selectionChangeHandler">
      <el-table-column :render-header="crud.renderHeader" type="selection" width="60"/>
      <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('code')" label="编码" prop="code"/>
      <!--      <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('dept')" prop="dept" label="所属部门">-->
      <!--        <template slot-scope="scope">-->
      <!--          <div>{{ scope.row.deptSuperiorName ? scope.row.deptSuperiorName + ' / ' : '' }}{{ scope.row.dept.name }}</div>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('name')" label="名称" prop="name"/>
      <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('phone')" label="联系电话" prop="phone"/>
      <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('remark')" label="备注" prop="remark"/>
      <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('seq')" label="排序" prop="seq">
        <template slot-scope="scope">
          {{ scope.row.seq }}
        </template>
      </el-table-column>
      <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('status')" align="center" label="状态" prop="status">
        <template slot-scope="scope">
          <el-switch
              v-model="scope.row.status == 1"
              active-color="#409EFF"
              inactive-color="#F56C6C"
              @change="changeEnabled(scope.row, scope.row.status)"
          />
        </template>
      </el-table-column>
      <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('createTime')" label="创建日期" prop="createTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <!--   编辑与删除   -->
      <el-table-column
          v-permission="['admin','job:edit','job:del']"
          align="center"
          fixed="right"
          label="操作"
          width="130px"
      >
        <template slot-scope="scope">
          <udOperation
              :data="scope.row"
              :disabledDle="scope.row.enabled"
              :permission="permission"
          />
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <pagination/>
    <!--表单渲染-->
    <eForm :job-status="dict.job_status"/>
  </div>
</template>

<script>
import crudJob from '@/api/system/job'
import eHeader from './module/header'
import eForm from './module/form'
import CRUD, {presenter} from '@crud/crud'
import crudOperation from '@crud/CRUD.operation'
import pagination from '@crud/Pagination'
import udOperation from '@crud/UD.operation'

// crud交由presenter持有
const crud = CRUD({
  title: '岗位',
  url: 'api/sysPost',
  crudMethod: {...crudJob}
})

export default {
  name: 'sysPost',
  components: {eHeader, eForm, crudOperation, pagination, udOperation},
  mixins: [presenter(crud)],
  // 数据字典
  dicts: ['job_status'],
  data() {
    return {
      permission: {
        add: ['admin', 'sysPost:add'],
        edit: ['admin', 'sysPost:edit'],
        del: ['admin', 'sysPost:del'],
        import: ['admin', 'sysPost:import'],
        export: ['admin', 'sysPost:export'],
        enable: ['admin', 'sysPost:enable'],
        disable: ['admin', 'sysPost:disable'],
      },
      job_status: [{value: 'true', label: '正常'},
        {value: 'false', label: '禁用'}]
    }
  },
  methods: {
    // 改变状态
    changeEnabled(data, val) {
      let info = val == 0 ? '启用' : '禁止'
      this.$confirm('此操作将 "' + info + '" ' + data.name + '岗位, 是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        data.status = val == 0 ? 1 : 0
        crud.crudMethod.edit(data).then(() => {
          crud.notify(info + '成功', 'success')
        }).catch(err => {
        })
      }).catch(() => {
      })
    }
  }
}
</script>


